<template>
  <div class="calendar-container">
    <el-calendar v-model="now">
      <template
        slot="dateCell"
        slot-scope="{date, data}"
      >
        <div class="calendar-day">
          <div
            class="calendar-day__box"
            :class="dateClasses(date, data)"
          >
            <div class="calendar-day__box-date">{{ date.getDate() }}</div>
            <div class="calendar-day__box-text">{{ dateMeta(date, data) }}</div>
          </div>
        </div>
      </template>
    </el-calendar>
  </div>
</template>
<script>
export default {
  name: 'Calendar',
  components: {},
  props: {
    value: {
      type: Date,
      default: () => {
        return new Date()
      }
    },
    metaDates: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    return {
      selectedDates: [],
      holiday: []
    }
  },
  computed: {
    now: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit('input', val)
      }
    },
    year() {
      return this.value.getFullYear()
    },
    holidayDates() {
      var dates = {}
      this.metaDates.map(item => {
        dates[item.date] = item
      })
      return dates
    }
  },
  methods: {
    // 是否节假日
    isFestival(slotDate, slotData) {
      var holiday = this.holidayDates[slotData.day]
      return holiday && holiday.workday
    },
    dateClasses(slotDate, slotData) {
      var holiday = this.holidayDates[slotData.day]
      if (holiday) {
        return {
          holiday: !holiday.workday,
          overtime: holiday.overtime
        }
      }
      return null
    },
    dateMeta(slotDate, slotData) {
      var holiday = this.holidayDates[slotData.day]
      var text = ''
      if (holiday) {
        if (holiday.holiday) {
          text = holiday.holiday
        } else if (holiday.date === this.$moment().format('YYYY-MM-DD')) {
          text = '今天'
        }
      }
      return text
    }
  }
}
</script>
<style lang="scss" scoped>
.calendar {
  &-container {
    font-family: Arial, Microsoft Yahei, sans-serif;
    ::v-deep .el-calendar {
      width: 600px;
      border: 1px solid #dfe6ec;
      border-radius: 5px;
      &__body {
        padding: 10px;
      }
      &-day {
        padding: 0;
        height: auto;
        position: relative;
        &::before {
          padding-top: 100%;
          content: '';
          display: block;
        }
      }
      &-table {
        thead > th {
          text-align: center;
        }
        td {
          border-color: transparent;
          &.is-selected,
          > *:hover {
            cursor: default;
            background: transparent;
          }
        }
      }
      &-table:not(.is-range) td.next,
      &-table:not(.is-range) td.prev {
        opacity: 0.4;
      }
    }
  }
  &-day {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    padding: 3px;
    &__box {
      width: 100%;
      height: 100%;
      display: flex;
      align-items: center;
      justify-content: center;
      flex-direction: column;
      border-radius: 5px;
      border: 2px solid transparent;
      position: relative;
      padding: 10%;
      &:hover {
        cursor: pointer;
        border-color: #bdbfc8;
      }
      &::before {
        position: absolute;
        left: 4px;
        top: 4px;
        font-size: 12px;
        line-height: 12px;
      }
      &-date {
        flex: 2;
        display: flex;
        align-items: center;
        font-size: 20px;
        font-weight: 500;
      }
      &-text {
        flex: 1;
        font-size: 12px;
        display: flex;
        align-items: center;
        color: #666;
      }
    }
  }
}
.holiday {
  background: #fde3e4;
  color: #f73131;
  &::before {
    content: '休';
    color: #f73131;
  }
}
.overtime {
  background: #f5f5f6;
  &::before {
    content: '班';
    color: #626675;
  }
}
td.is-selected .calendar-day__box,
.calendar-day__box:hover {
  border-color: #bdbfc8;
  &.holiday {
    border-color: #f38686;
  }
}
.square-box {
  > * {
    border: 2px solid transparent;
    padding: 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    &::before {
      position: absolute;
      left: 10px;
      top: 10px;
    }
    h2 {
      margin: 0;
      flex: 2;
      line-height: 1;
      align-self: center;
    }
    p {
      margin: 0;
      font-size: 12px;
      color: gray;
      flex: 1;
      line-height: 1;
      align-self: center;
    }
  }
}
.is-selected {
  border: 2px solid #f660f6;
}
</style>
